﻿using Microsoft.AspNetCore.Components.Forms;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Modules.Project.Repositories;
using PmSoft;
using PmSoft.ApplicationContext;
using PmSoft.Exceptions;

namespace Modules.Project
{
    [UseDI(ServiceLifetime.Scoped)]
    public class ProjectGuaranteeRefundService
    {
        private readonly ProjectGuaranteeRefundRepository repository;
        private readonly IApplicationContext applicationContext;
        private readonly ProjectGuaranteePaymentService projectGuaranteePaymentService;
        private readonly ILogger logger;

        public ProjectGuaranteeRefundService(ProjectGuaranteeRefundRepository repository, IApplicationContext applicationContext, ILogger<ProjectGuaranteeRefundService> logger, ProjectGuaranteePaymentService projectGuaranteePaymentService)
        {
            this.repository = repository;
            this.applicationContext = applicationContext;
            this.logger = logger;
            this.projectGuaranteePaymentService = projectGuaranteePaymentService;
        }

        public ProjectGuaranteeRefund? GetProjectGuaranteeRefund(int id)
        {
            return repository.Get(id);
        }

        public void Create(ProjectGuaranteeRefund entity)
        {
            repository.Insert(entity);
        }

        public void Update(ProjectGuaranteeRefund entity)
        {
            repository.Update(entity);
        }

        /// <summary>
        /// 退还缴纳信息
        /// </summary>
        /// <param name="entity"></param>
        public void RefundPayment(ProjectGuaranteeRefund entity)
        {
            var payment = projectGuaranteePaymentService.GetProjectGuaranteePayment(entity.GuaranteePaymentId ?? 0);
            if (payment == null)
            {
                throw new BusinessException("缴纳信息不存在");
            }
            try
            {
                applicationContext.Transaction.Begin();
                if (entity.RefundTime <= DateTime.Now)
                {
                    entity.Status = 保证金退还状态.已退还;
                }
                else
                {
                    entity.Status = 保证金退还状态.待退还;
                }
                repository.Update(entity);
                payment.HasReturn = true;
                projectGuaranteePaymentService.Update(payment);
                applicationContext.Transaction.Complete();
            }
            catch (Exception ex)
            {
                applicationContext.Transaction.Abort();
                logger.LogError(ex, "退还缴纳信息失败");
                throw new BusinessException("退还缴纳信息失败");
            }
        }

        public IPagedList<ProjectGuaranteeRefund> GetProjectGuaranteeRefunds(ProjectGuaranteeRefundQuery query, int pageIndex, int pageSize)
        {
            return repository.GetProjectGuaranteeRefunds(query, pageIndex, pageSize);
        }

        public void Delete(int id)
        {
            repository.DeleteByEntityId(id);
        }

        public void Deleted(IEnumerable<int> ids)
        {
            foreach (int id in ids)
            {
                Delete(id);
            }
        }

        public bool ExistProjectGuaranteeRefund(int guaranteePaymentId)
        {
            return repository.ExistProjectGuaranteeRefund(guaranteePaymentId);
        }

        public ProjectGuaranteeRefund? GetProjectGuaranteeRefundByGuaranteePaymentId(int guaranteePaymentId)
        {
            return repository.GetProjectGuaranteeRefundByGuaranteePaymentId(guaranteePaymentId);
        }

        public IEnumerable<ProjectGuaranteeRefund> GetProjectGuaranteeRefundsByStatus(保证金退还状态 status)
        {
            return repository.GetProjectGuaranteeRefundsByStatus(status);
        }
    }
}